package com.share.device.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.utils.StringUtils;
import com.share.device.domain.Region;
import com.share.device.mapper.RegionMapper;
import com.share.device.service.IRegionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author HSL
 * @version 1.0
 * @data 2025/8/29 11:28
 * @desc todo
 */

@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {
    @Resource
    private RegionMapper regionMapper;

    @Override
    public List<Region> selectTree(String parentCode) {
        List<Region> regionList = regionMapper.selectList(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, parentCode));
        if (CollectionUtil.isNotEmpty(regionList)) {
            regionList.forEach(region -> {
                if (regionMapper.selectCount(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, region.getCode())) > 0) {
                    region.setHasChildren(true);
                } else {
                    region.setHasChildren(false);
                }
            });
        }
        return regionList;
    }

    @Override
    public String getNameByCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return "";
        } else {
            String name = regionMapper.selectOne(new LambdaQueryWrapper<Region>().eq(Region::getCode, code)).getName();
            if (StringUtils.isEmpty(name)) {
                return "";
            } else {
                return name;
            }
        }
    }
}
